package com.ly.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.pojo.ReturnPerson;
import com.ly.service.ReturnPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @auth 吴阳基
 * @date 2024/2/3 14:12
 */
@Controller
@RequestMapping
public class ReturnPersonController {
    @Autowired
    private ReturnPersonService returnPersonService;

    @GetMapping("/list")
    public String list(Model model,
                       @RequestParam(required = false) String returnDate,
                       @RequestParam(defaultValue = "1", required = false) Integer pageNo) {
        Integer pageSize = 3;

        QueryWrapper<ReturnPerson> queryWrapper = new QueryWrapper<>();

        if (returnDate != null && !returnDate.equals("")) {
            queryWrapper.like("returnDate", returnDate);
        }

        IPage<ReturnPerson> page = returnPersonService.page(new Page<>(pageNo, pageSize), queryWrapper);
        long totalCount = page.getTotal();
        long totalPages = page.getPages();
        List<ReturnPerson> list = page.getRecords();


        model.addAttribute("list", list);
        model.addAttribute("returnDate", returnDate);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("totalPages", totalPages);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("pageSize", pageSize);

        return "list";
    }

    @PostMapping("/add")
    public String add(ReturnPerson returnPerson,
                      BindingResult result) {
        if (result.hasErrors()) {
            return "add";
        }

        returnPersonService.save(returnPerson);
        return "redirect:/list";
    }

    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("returnPerson", new ReturnPerson());
        return "add";
    }

    @GetMapping("/update")
    public String byId(Model model,
                       @RequestParam Integer id) {
        ReturnPerson returnPerson = returnPersonService.getById(id);
        model.addAttribute("returnPerson", returnPerson);
        return "update";
    }

    @PostMapping("/update")
    public String update(ReturnPerson returnPerson,
                         BindingResult result) {
        if (result.hasErrors()) {
            return "update";
        }

        returnPersonService.updateById(returnPerson);
        return "redirect:/list";
    }

    @GetMapping("/delete")
    public String delete(@RequestParam Integer id) {
        returnPersonService.removeById(id);
        return "redirect:/list";
    }
}